﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SecsaMvcApplication.Models;
using PagedList.Mvc;
using PagedList;

namespace SecsaMvcApplication.Controllers
{
    public class MedicosController : Controller
    {
        private DataContext db = new DataContext();

        //
        // GET: /Medicos/

        public ActionResult Index(int? page)
        {
            var pageNumber = page ?? 1;
            return View(db.Medicos.OrderBy(entity => entity.Id).ToPagedList(pageNumber, 20));
        }

        //
        // GET: /Medicos/Details/5

        public ActionResult Details(int id = 0)
        {
            Medico medico = db.Medicos.Find(id);
            if (medico == null)
            {
                return HttpNotFound();
            }
            return View(medico);
        }

        //
        // GET: /Medicos/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Medicos/Create

        [HttpPost]
        public ActionResult Create(Medico medico)
        {
            if (ModelState.IsValid)
            {
                db.Medicos.Add(medico);
                db.SaveChanges();
                TempData["Notificacao"] = new NotificacaoViewModel(
                  "O registro foi salvo", "success"
              );
                return RedirectToAction("Index");
            }

            return View(medico);
        }

        //
        // GET: /Medicos/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Medico medico = db.Medicos.Find(id);
            if (medico == null)
            {
                return HttpNotFound();
            }
            return View(medico);
        }

        //
        // POST: /Medicos/Edit/5

        [HttpPost]
        public ActionResult Edit(Medico medico)
        {
            if (ModelState.IsValid)
            {
                db.Entry(medico).State = EntityState.Modified;
                db.SaveChanges();
                TempData["Notificacao"] = new NotificacaoViewModel(
                    "O registro foi atualizado", "success"
                );
                return RedirectToAction("Index");
            }
            return View(medico);
        }

        //
        // GET: /Medicos/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Medico medico = db.Medicos.Find(id);
            if (medico == null)
            {
                return HttpNotFound();
            }
            return View(medico);
        }

        //
        // POST: /Medicos/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Medico medico = db.Medicos.Find(id);
            db.Medicos.Remove(medico);
            db.SaveChanges();
            TempData["Notificacao"] = new NotificacaoViewModel(
                "O registro foi apagado", "success"
            );
            return RedirectToAction("Index");
        }

        //
        // GET: /Pacientes/FindByName/Francisc

        public JsonResult FindByName(string query)
        {
            var result = db.Medicos.Where(entity => entity.Nome.Contains(query)).ToList();
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}